Order Management

Create an Order for an Account


In [1]:
import pandas as pd
import oandapyV20
import oandapyV20.endpoints.orders as orders
import configparser

In [2]:
config = configparser.ConfigParser()
config.read('../config/config_v20.ini')
accountID = config['oanda']['account_id']
access_token = config['oanda']['api_key']

In [3]:
client = oandapyV20.API(access_token=access_token)

In [4]:
data = {
  "order": {
    "price": "1.2",
    "stopLossOnFill": {
      "timeInForce": "GTC",
      "price": "1.22"
    },
    "timeInForce": "GTC",
    "instrument": "EUR_USD",
    "units": "-100",
    "type": "LIMIT",
    "positionFill": "DEFAULT"
  }
}

In [5]:
r = orders.OrderCreate(accountID, data=data)

In [6]:
client.request(r)


Out[6]:
{'lastTransactionID': '69',
 'orderCreateTransaction': {'accountID': '101-003-5120068-001',
  'batchID': '69',
  'id': '69',
  'instrument': 'EUR_USD',
  'positionFill': 'DEFAULT',
  'price': '1.20000',
  'reason': 'CLIENT_ORDER',
  'stopLossOnFill': {'price': '1.22000', 'timeInForce': 'GTC'},
  'time': '2017-01-30T01:04:28.187803832Z',
  'timeInForce': 'GTC',
  'triggerCondition': 'TRIGGER_DEFAULT',
  'type': 'LIMIT_ORDER',
  'units': '-100',
  'userID': 5120068},
 'relatedTransactionIDs': ['69']}

In [7]:
print(r.response)


{'relatedTransactionIDs': ['69'], 'orderCreateTransaction': {'time': '2017-01-30T01:04:28.187803832Z', 'units': '-100', 'accountID': '101-003-5120068-001', 'id': '69', 'stopLossOnFill': {'price': '1.22000', 'timeInForce': 'GTC'}, 'instrument': 'EUR_USD', 'price': '1.20000', 'triggerCondition': 'TRIGGER_DEFAULT', 'positionFill': 'DEFAULT', 'timeInForce': 'GTC', 'type': 'LIMIT_ORDER', 'userID': 5120068, 'batchID': '69', 'reason': 'CLIENT_ORDER'}, 'lastTransactionID': '69'}

In [8]:
pd.Series(r.response['orderCreateTransaction'])


Out[8]:
accountID                                  101-003-5120068-001
batchID                                                     69
id                                                          69
instrument                                             EUR_USD
positionFill                                           DEFAULT
price                                                  1.20000
reason                                            CLIENT_ORDER
stopLossOnFill      {'price': '1.22000', 'timeInForce': 'GTC'}
time                            2017-01-30T01:04:28.187803832Z
timeInForce                                                GTC
triggerCondition                               TRIGGER_DEFAULT
type                                               LIMIT_ORDER
units                                                     -100
userID                                                 5120068
dtype: object

Get a List of Orders for an Account


In [9]:
r = orders.OrderList(accountID)

In [10]:
client.request(r)


Out[10]:
{'lastTransactionID': '69',
 'orders': [{'createTime': '2017-01-30T01:04:28.187803832Z',
   'id': '69',
   'instrument': 'EUR_USD',
   'partialFill': 'DEFAULT_FILL',
   'positionFill': 'POSITION_DEFAULT',
   'price': '1.20000',
   'state': 'PENDING',
   'stopLossOnFill': {'price': '1.22000', 'timeInForce': 'GTC'},
   'timeInForce': 'GTC',
   'triggerCondition': 'TRIGGER_DEFAULT',
   'type': 'LIMIT',
   'units': '-100'},
  {'createTime': '2017-01-20T15:47:51.120880289Z',
   'id': '17',
   'instrument': 'EUR_USD',
   'partialFill': 'DEFAULT_FILL',
   'positionFill': 'POSITION_DEFAULT',
   'price': '1.25000',
   'replacesOrderID': '15',
   'state': 'PENDING',
   'timeInForce': 'GTC',
   'triggerCondition': 'TRIGGER_DEFAULT',
   'type': 'LIMIT',
   'units': '-500000'},
  {'createTime': '2017-01-20T15:47:33.998386716Z',
   'id': '13',
   'instrument': 'EUR_USD',
   'partialFill': 'DEFAULT_FILL',
   'positionFill': 'POSITION_DEFAULT',
   'price': '1.25000',
   'replacesOrderID': '11',
   'state': 'PENDING',
   'timeInForce': 'GTC',
   'triggerCondition': 'TRIGGER_DEFAULT',
   'type': 'LIMIT',
   'units': '-500000'},
  {'createTime': '2017-01-20T15:44:35.046525739Z',
   'id': '9',
   'instrument': 'EUR_USD',
   'partialFill': 'DEFAULT_FILL',
   'positionFill': 'POSITION_DEFAULT',
   'price': '1.20000',
   'state': 'PENDING',
   'stopLossOnFill': {'price': '1.22000', 'timeInForce': 'GTC'},
   'timeInForce': 'GTC',
   'triggerCondition': 'TRIGGER_DEFAULT',
   'type': 'LIMIT',
   'units': '-100'}]}

In [11]:
print(r.response)


{'lastTransactionID': '69', 'orders': [{'state': 'PENDING', 'units': '-100', 'id': '69', 'stopLossOnFill': {'price': '1.22000', 'timeInForce': 'GTC'}, 'instrument': 'EUR_USD', 'createTime': '2017-01-30T01:04:28.187803832Z', 'triggerCondition': 'TRIGGER_DEFAULT', 'positionFill': 'POSITION_DEFAULT', 'timeInForce': 'GTC', 'type': 'LIMIT', 'partialFill': 'DEFAULT_FILL', 'price': '1.20000'}, {'state': 'PENDING', 'createTime': '2017-01-20T15:47:51.120880289Z', 'replacesOrderID': '15', 'id': '17', 'positionFill': 'POSITION_DEFAULT', 'instrument': 'EUR_USD', 'units': '-500000', 'triggerCondition': 'TRIGGER_DEFAULT', 'timeInForce': 'GTC', 'type': 'LIMIT', 'partialFill': 'DEFAULT_FILL', 'price': '1.25000'}, {'state': 'PENDING', 'createTime': '2017-01-20T15:47:33.998386716Z', 'replacesOrderID': '11', 'id': '13', 'positionFill': 'POSITION_DEFAULT', 'instrument': 'EUR_USD', 'units': '-500000', 'triggerCondition': 'TRIGGER_DEFAULT', 'timeInForce': 'GTC', 'type': 'LIMIT', 'partialFill': 'DEFAULT_FILL', 'price': '1.25000'}, {'state': 'PENDING', 'units': '-100', 'id': '9', 'stopLossOnFill': {'price': '1.22000', 'timeInForce': 'GTC'}, 'instrument': 'EUR_USD', 'createTime': '2017-01-20T15:44:35.046525739Z', 'triggerCondition': 'TRIGGER_DEFAULT', 'positionFill': 'POSITION_DEFAULT', 'timeInForce': 'GTC', 'type': 'LIMIT', 'partialFill': 'DEFAULT_FILL', 'price': '1.20000'}]}

In [12]:
pd.Series(r.response['orders'][0])


Out[12]:
createTime                      2017-01-30T01:04:28.187803832Z
id                                                          69
instrument                                             EUR_USD
partialFill                                       DEFAULT_FILL
positionFill                                  POSITION_DEFAULT
price                                                  1.20000
state                                                  PENDING
stopLossOnFill      {'price': '1.22000', 'timeInForce': 'GTC'}
timeInForce                                                GTC
triggerCondition                               TRIGGER_DEFAULT
type                                                     LIMIT
units                                                     -100
dtype: object

List all Pending Orders in an Account


In [13]:
r = orders.OrdersPending(accountID)

In [14]:
client.request(r)


Out[14]:
{'lastTransactionID': '69',
 'orders': [{'createTime': '2017-01-30T01:04:28.187803832Z',
   'id': '69',
   'instrument': 'EUR_USD',
   'partialFill': 'DEFAULT_FILL',
   'positionFill': 'POSITION_DEFAULT',
   'price': '1.20000',
   'state': 'PENDING',
   'stopLossOnFill': {'price': '1.22000', 'timeInForce': 'GTC'},
   'timeInForce': 'GTC',
   'triggerCondition': 'TRIGGER_DEFAULT',
   'type': 'LIMIT',
   'units': '-100'},
  {'createTime': '2017-01-20T15:47:51.120880289Z',
   'id': '17',
   'instrument': 'EUR_USD',
   'partialFill': 'DEFAULT_FILL',
   'positionFill': 'POSITION_DEFAULT',
   'price': '1.25000',
   'replacesOrderID': '15',
   'state': 'PENDING',
   'timeInForce': 'GTC',
   'triggerCondition': 'TRIGGER_DEFAULT',
   'type': 'LIMIT',
   'units': '-500000'},
  {'createTime': '2017-01-20T15:47:33.998386716Z',
   'id': '13',
   'instrument': 'EUR_USD',
   'partialFill': 'DEFAULT_FILL',
   'positionFill': 'POSITION_DEFAULT',
   'price': '1.25000',
   'replacesOrderID': '11',
   'state': 'PENDING',
   'timeInForce': 'GTC',
   'triggerCondition': 'TRIGGER_DEFAULT',
   'type': 'LIMIT',
   'units': '-500000'},
  {'createTime': '2017-01-20T15:44:35.046525739Z',
   'id': '9',
   'instrument': 'EUR_USD',
   'partialFill': 'DEFAULT_FILL',
   'positionFill': 'POSITION_DEFAULT',
   'price': '1.20000',
   'state': 'PENDING',
   'stopLossOnFill': {'price': '1.22000', 'timeInForce': 'GTC'},
   'timeInForce': 'GTC',
   'triggerCondition': 'TRIGGER_DEFAULT',
   'type': 'LIMIT',
   'units': '-100'}]}

In [15]:
print(r.response)


{'lastTransactionID': '69', 'orders': [{'state': 'PENDING', 'units': '-100', 'id': '69', 'stopLossOnFill': {'price': '1.22000', 'timeInForce': 'GTC'}, 'instrument': 'EUR_USD', 'createTime': '2017-01-30T01:04:28.187803832Z', 'triggerCondition': 'TRIGGER_DEFAULT', 'positionFill': 'POSITION_DEFAULT', 'timeInForce': 'GTC', 'type': 'LIMIT', 'partialFill': 'DEFAULT_FILL', 'price': '1.20000'}, {'state': 'PENDING', 'createTime': '2017-01-20T15:47:51.120880289Z', 'replacesOrderID': '15', 'id': '17', 'positionFill': 'POSITION_DEFAULT', 'instrument': 'EUR_USD', 'units': '-500000', 'triggerCondition': 'TRIGGER_DEFAULT', 'timeInForce': 'GTC', 'type': 'LIMIT', 'partialFill': 'DEFAULT_FILL', 'price': '1.25000'}, {'state': 'PENDING', 'createTime': '2017-01-20T15:47:33.998386716Z', 'replacesOrderID': '11', 'id': '13', 'positionFill': 'POSITION_DEFAULT', 'instrument': 'EUR_USD', 'units': '-500000', 'triggerCondition': 'TRIGGER_DEFAULT', 'timeInForce': 'GTC', 'type': 'LIMIT', 'partialFill': 'DEFAULT_FILL', 'price': '1.25000'}, {'state': 'PENDING', 'units': '-100', 'id': '9', 'stopLossOnFill': {'price': '1.22000', 'timeInForce': 'GTC'}, 'instrument': 'EUR_USD', 'createTime': '2017-01-20T15:44:35.046525739Z', 'triggerCondition': 'TRIGGER_DEFAULT', 'positionFill': 'POSITION_DEFAULT', 'timeInForce': 'GTC', 'type': 'LIMIT', 'partialFill': 'DEFAULT_FILL', 'price': '1.20000'}]}

In [16]:
res = r.response['orders']

In [17]:
print(res)


[{'state': 'PENDING', 'units': '-100', 'id': '69', 'stopLossOnFill': {'price': '1.22000', 'timeInForce': 'GTC'}, 'instrument': 'EUR_USD', 'createTime': '2017-01-30T01:04:28.187803832Z', 'triggerCondition': 'TRIGGER_DEFAULT', 'positionFill': 'POSITION_DEFAULT', 'timeInForce': 'GTC', 'type': 'LIMIT', 'partialFill': 'DEFAULT_FILL', 'price': '1.20000'}, {'state': 'PENDING', 'createTime': '2017-01-20T15:47:51.120880289Z', 'replacesOrderID': '15', 'id': '17', 'positionFill': 'POSITION_DEFAULT', 'instrument': 'EUR_USD', 'units': '-500000', 'triggerCondition': 'TRIGGER_DEFAULT', 'timeInForce': 'GTC', 'type': 'LIMIT', 'partialFill': 'DEFAULT_FILL', 'price': '1.25000'}, {'state': 'PENDING', 'createTime': '2017-01-20T15:47:33.998386716Z', 'replacesOrderID': '11', 'id': '13', 'positionFill': 'POSITION_DEFAULT', 'instrument': 'EUR_USD', 'units': '-500000', 'triggerCondition': 'TRIGGER_DEFAULT', 'timeInForce': 'GTC', 'type': 'LIMIT', 'partialFill': 'DEFAULT_FILL', 'price': '1.25000'}, {'state': 'PENDING', 'units': '-100', 'id': '9', 'stopLossOnFill': {'price': '1.22000', 'timeInForce': 'GTC'}, 'instrument': 'EUR_USD', 'createTime': '2017-01-20T15:44:35.046525739Z', 'triggerCondition': 'TRIGGER_DEFAULT', 'positionFill': 'POSITION_DEFAULT', 'timeInForce': 'GTC', 'type': 'LIMIT', 'partialFill': 'DEFAULT_FILL', 'price': '1.20000'}]

In [18]:
last_order_id = res[0]['id']

In [19]:
pd.Series(r.response['orders'][0])


Out[19]:
createTime                      2017-01-30T01:04:28.187803832Z
id                                                          69
instrument                                             EUR_USD
partialFill                                       DEFAULT_FILL
positionFill                                  POSITION_DEFAULT
price                                                  1.20000
state                                                  PENDING
stopLossOnFill      {'price': '1.22000', 'timeInForce': 'GTC'}
timeInForce                                                GTC
triggerCondition                               TRIGGER_DEFAULT
type                                                     LIMIT
units                                                     -100
dtype: object

Get Details for a Single Order in an Account


In [20]:
r = orders.OrderDetails(accountID=accountID, orderID=last_order_id)

In [21]:
client.request(r)


Out[21]:
{'lastTransactionID': '69',
 'order': {'createTime': '2017-01-30T01:04:28.187803832Z',
  'id': '69',
  'instrument': 'EUR_USD',
  'partialFill': 'DEFAULT_FILL',
  'positionFill': 'POSITION_DEFAULT',
  'price': '1.20000',
  'state': 'PENDING',
  'stopLossOnFill': {'price': '1.22000', 'timeInForce': 'GTC'},
  'timeInForce': 'GTC',
  'triggerCondition': 'TRIGGER_DEFAULT',
  'type': 'LIMIT',
  'units': '-100'}}

Replace an Order in an Account by simultaneously cancelling it and createing a replacement Order.


In [22]:
data = {
  "order": {
    "units": "-500000",
    "instrument": "EUR_USD",
    "price": "1.25000",
    "type": "LIMIT"
  }
}

In [23]:
r = orders.OrderReplace(accountID=accountID, orderID=last_order_id, data=data)

In [24]:
client.request(r)


Out[24]:
{'lastTransactionID': '71',
 'orderCancelTransaction': {'accountID': '101-003-5120068-001',
  'batchID': '70',
  'id': '70',
  'orderID': '69',
  'reason': 'CLIENT_REQUEST_REPLACED',
  'replacedByOrderID': '71',
  'time': '2017-01-30T01:04:29.935077629Z',
  'type': 'ORDER_CANCEL',
  'userID': 5120068},
 'orderCreateTransaction': {'accountID': '101-003-5120068-001',
  'batchID': '70',
  'id': '71',
  'instrument': 'EUR_USD',
  'positionFill': 'DEFAULT',
  'price': '1.25000',
  'reason': 'REPLACEMENT',
  'replacesOrderID': '69',
  'time': '2017-01-30T01:04:29.935077629Z',
  'timeInForce': 'GTC',
  'triggerCondition': 'TRIGGER_DEFAULT',
  'type': 'LIMIT_ORDER',
  'units': '-500000',
  'userID': 5120068},
 'relatedTransactionIDs': ['70', '71']}

In [25]:
print(r.response)


{'relatedTransactionIDs': ['70', '71'], 'orderCreateTransaction': {'time': '2017-01-30T01:04:29.935077629Z', 'units': '-500000', 'replacesOrderID': '69', 'accountID': '101-003-5120068-001', 'id': '71', 'positionFill': 'DEFAULT', 'instrument': 'EUR_USD', 'price': '1.25000', 'triggerCondition': 'TRIGGER_DEFAULT', 'timeInForce': 'GTC', 'type': 'LIMIT_ORDER', 'userID': 5120068, 'batchID': '70', 'reason': 'REPLACEMENT'}, 'lastTransactionID': '71', 'orderCancelTransaction': {'time': '2017-01-30T01:04:29.935077629Z', 'accountID': '101-003-5120068-001', 'type': 'ORDER_CANCEL', 'replacedByOrderID': '71', 'userID': 5120068, 'batchID': '70', 'orderID': '69', 'reason': 'CLIENT_REQUEST_REPLACED', 'id': '70'}}

In [26]:
req_id = r.response['lastTransactionID']

Cancel a pending Order in an Account.


In [27]:
r = orders.OrderCancel(accountID=accountID, orderID=req_id)

In [28]:
client.request(r)


Out[28]:
{'lastTransactionID': '72',
 'orderCancelTransaction': {'accountID': '101-003-5120068-001',
  'batchID': '72',
  'id': '72',
  'orderID': '71',
  'reason': 'CLIENT_REQUEST',
  'time': '2017-01-30T01:04:30.314623316Z',
  'type': 'ORDER_CANCEL',
  'userID': 5120068},
 'relatedTransactionIDs': ['72']}

In [29]:
print(r.response)


{'relatedTransactionIDs': ['72'], 'lastTransactionID': '72', 'orderCancelTransaction': {'time': '2017-01-30T01:04:30.314623316Z', 'accountID': '101-003-5120068-001', 'type': 'ORDER_CANCEL', 'userID': 5120068, 'batchID': '72', 'orderID': '71', 'reason': 'CLIENT_REQUEST', 'id': '72'}}

In [30]:
last_order_id


Out[30]:
'69'

MKT Order


In [31]:
data = {"order": 
        {"units": "100", 
         "instrument": "GBP_USD", 
         "timeInForce": "FOK", 
         "type": "MARKET", 
         "positionFill": "DEFAULT"
        },
       }

In [32]:
r = orders.OrderCreate(accountID, data=data)

In [33]:
client.request(r)


Out[33]:
{'lastTransactionID': '74',
 'orderCreateTransaction': {'accountID': '101-003-5120068-001',
  'batchID': '73',
  'id': '73',
  'instrument': 'GBP_USD',
  'positionFill': 'DEFAULT',
  'reason': 'CLIENT_ORDER',
  'time': '2017-01-30T01:04:30.659975082Z',
  'timeInForce': 'FOK',
  'type': 'MARKET_ORDER',
  'units': '100',
  'userID': 5120068},
 'orderFillTransaction': {'accountBalance': '100000.3090',
  'accountID': '101-003-5120068-001',
  'batchID': '73',
  'financing': '0.0000',
  'id': '74',
  'instrument': 'GBP_USD',
  'orderID': '73',
  'pl': '0.0000',
  'price': '1.25892',
  'reason': 'MARKET_ORDER',
  'time': '2017-01-30T01:04:30.659975082Z',
  'tradeOpened': {'tradeID': '74', 'units': '100'},
  'type': 'ORDER_FILL',
  'units': '100',
  'userID': 5120068},
 'relatedTransactionIDs': ['73', '74']}

In [34]:
print(r.response)


{'relatedTransactionIDs': ['73', '74'], 'orderCreateTransaction': {'time': '2017-01-30T01:04:30.659975082Z', 'timeInForce': 'FOK', 'id': '73', 'accountID': '101-003-5120068-001', 'type': 'MARKET_ORDER', 'positionFill': 'DEFAULT', 'userID': 5120068, 'batchID': '73', 'units': '100', 'reason': 'CLIENT_ORDER', 'instrument': 'GBP_USD'}, 'lastTransactionID': '74', 'orderFillTransaction': {'time': '2017-01-30T01:04:30.659975082Z', 'units': '100', 'accountID': '101-003-5120068-001', 'id': '74', 'pl': '0.0000', 'instrument': 'GBP_USD', 'batchID': '73', 'price': '1.25892', 'type': 'ORDER_FILL', 'accountBalance': '100000.3090', 'userID': 5120068, 'tradeOpened': {'tradeID': '74', 'units': '100'}, 'orderID': '73', 'financing': '0.0000', 'reason': 'MARKET_ORDER'}}

In [35]:
pd.Series(r.response['orderCreateTransaction'])


Out[35]:
accountID                  101-003-5120068-001
batchID                                     73
id                                          73
instrument                             GBP_USD
positionFill                           DEFAULT
reason                            CLIENT_ORDER
time            2017-01-30T01:04:30.659975082Z
timeInForce                                FOK
type                              MARKET_ORDER
units                                      100
userID                                 5120068
dtype: object

In [36]:
pd.Series(r.response['orderFillTransaction'])


Out[36]:
accountBalance                          100000.3090
accountID                       101-003-5120068-001
batchID                                          73
financing                                    0.0000
id                                               74
instrument                                  GBP_USD
orderID                                          73
pl                                           0.0000
price                                       1.25892
reason                                 MARKET_ORDER
time                 2017-01-30T01:04:30.659975082Z
tradeOpened       {'tradeID': '74', 'units': '100'}
type                                     ORDER_FILL
units                                           100
userID                                      5120068
dtype: object